{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 多元线性回归\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "from numpy.linalg import inv\n",
    "from sklearn.linear_model import LinearRegression\n",
    "\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 场景如下\n",
    "# y = a * x1 + b * x2 + c\n",
    "#                   | a |\n",
    "# y = [x1, x2, 1] * | b |\n",
    "#                   | c |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 简单讲下多元线性回归问题，就是对方程Y=Xβ求解β。\n",
    "# 如果是方阵的话就很简单：β = X逆 * Y。\n",
    "# 但是事实没这么简单，我们有另一个方法，可以用X.T * X把X变成方阵。\n",
    "# X的维度a * b，β的维度b * 1\n",
    "# Y的维度a * 1\n",
    "# X.T * X = X'后得到一个b * b的方阵。\n",
    "# X.T * Y后得到Y'，一个b * 1的矩阵。\n",
    "# Y' = X'β\n",
    "# X'逆 * Y' = β"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.],\n",
       "       [ 2.],\n",
       "       [ 3.]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = [[1,1,1],[1,1,2],[1,2,1]]\n",
    "y = [[6],[9],[8]]\n",
    "xt = np.transpose(x) # 转置\n",
    "xs = np.dot(xt, x) # 矩阵乘法\n",
    "ys = np.dot(xt, y)\n",
    "# inv求逆矩阵\n",
    "np.dot(inv(xs), ys) # y = 1 + 2 * x1 + 3 * x2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.  2.  3.]]\n",
      "[ 1.]\n"
     ]
    }
   ],
   "source": [
    "model = LinearRegression()\n",
    "model.fit(x, y)\n",
    "print(model.coef_)\n",
    "print(model.intercept_) # 变成y = ax1 + bx2 + cx3 + d形式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 22.]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x2 = [[1,3,5]]\n",
    "model.predict(x2)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
